package cz.techi;

import java.util.Calendar;
import java.util.Date;

public class Schedule {

	private static Schedule instance;
	
	private Subject[] subjects;
	
	private Schedule()
	{

	}
	
	public static Schedule getInstance()
	{
		if (instance == null)
		{
			instance = new Schedule();
		}
		
		return instance;
	}
	
	public void setSubjects(Subject[] subjects)
	{
		this.subjects = subjects;
	}
	
	public Subject[] getSubjects()
	{
		return subjects;
	}
	
	public Subject getCurrentSubject()
	{
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		
		for (Subject s: subjects)
		{
			if (s.getDay() == day)
			{
				if (date.after(s.getStart()) && date.before(s.getEnd()))
				{
					return s;
				}
			}
		}
		
		return null;
	}
	
	public Subject getNextSubject()
	{
		Calendar calendar = Calendar.getInstance();
		
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		
		Subject subject = null;
		
		for (int i = 0; i < 7; i++)
		{
			subject = getNextSubject(day);
			
			if (subject != null)
			{
				return subject;
			}
			
			
			
			if (day == Calendar.SATURDAY || day == Calendar.SUNDAY)
			{
				day = Calendar.MONDAY;
			}
			else
			{
				day++;
			}
		}
		
		return null;
	}
	
	private Subject getNextSubject(int day)
	{
		Date date = new Date();
		
		for (Subject s: subjects)
		{
			if (s.getDay() == day)
			{
				//android.util.Log.v("myShit", s.getStart().toGMTString());
				//android.util.Log.v("myShit", date.toGMTString());
				
				if (date.before(s.getStart()))
				{
					return s;
				}
								
			}
		}
		
		return null;
	}
}
